package com.as.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.as.base.BaseController;
import com.as.entity.User;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

	@ResponseBody
	@RequestMapping("/getUser/{userid}")
	public Map<String, Object> getUser(@PathVariable String userid) {
		System.out.println(userid);
		Map<String, Object> map = new HashMap<String, Object>();
		User user = userManager.getUser(userid);
		if (user != null) {
			map.put("states", "OK");
			map.put("user", user);
		} else {
			map.put("states", "ERROR");
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/getMe")
	public Map<String, Object> getMe(HttpSession httpSession) {
		User me = (User)httpSession.getAttribute("user");
		Map<String, Object> map = new HashMap<String, Object>();
		User user = userManager.getUser(me.getUserid());
		if (user != null) {
			map.put("states", "OK");
			map.put("user", user);
		} else {
			map.put("states", "ERROR");
		}
		return map;
	}

	@ResponseBody
	@RequestMapping("/getUserbyname")
	public Map<String, Object> getUserbyname(HttpSession httpSession,
			HttpServletRequest request) {
		System.out.println("----------:" + request.getParameter("username"));
		System.out.println("----------:" + request.getParameter("act"));
		System.out.println("----------:" + request.getRequestURL().toString());
		String username = request.getParameter("username");
		Map<String, Object> map = new HashMap<String, Object>();
		User user = userManager.getUserbyname(username);
		if (user != null) {
			System.out.println("+++++++++:" + user.getEmail());
			map.put("states", "OK");
			map.put("user", user);
		} else {
			map.put("states", "ERROR");
		}
		return map;
	}

	@ResponseBody
	@RequestMapping("/login/{username}/{password}")
	public Map<String, Object> login(@PathVariable String username,
			@PathVariable String password, HttpSession httpSession) {
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println(username + "  and  " + password);
		User user = userManager.login(username, password);
		if (user != null) {
			httpSession.setAttribute("user", user); // дsession
			map.put("states", "OK");
			map.put("user", user);
		} else {
			map.put("states", "ERROR");
			map.put("user", null);

		}
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/regist", method = { RequestMethod.POST })
	public Map<String, Object> regist(@RequestBody User userInfo,
			HttpSession httpSession) {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = userManager.getUserbyname(userInfo.getUsername());
		if (user != null) {
			map.put("states", "NAMEEXIST");
		} else if(userManager.getUserbyemail(userInfo.getEmail())!=null){
			map.put("states", "EMAILEXIST");
		}else
		{
			if (userManager.regist(userInfo)) {
				httpSession.setAttribute("user", userInfo); // дsession
				map.put("states", "OK");
				map.put("user", userInfo);
			} else {
				map.put("String", "ERROR");
				map.put("user", null);
			}
		}
		return map;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/addParent", method = { RequestMethod.POST })
	public Map<String, Object> addParent(@RequestBody User userInfo,
			HttpSession httpSession) {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = userManager.getUserbyname(userInfo.getUsername());
		User userLogin = (User)httpSession.getAttribute("user");
		userInfo.setRelateid(userLogin.getUserid());
		if (user != null) {
			map.put("states", "NAMEEXIST");
		} else if(userManager.getUserbyemail(userInfo.getEmail())!=null){
			map.put("states", "EMAILEXIST");
		}else
		{
			if (userManager.regist(userInfo)) {
				userLogin.setRelateid(userInfo.getUserid());
				userManager.updateUser(userLogin);
				map.put("states", "OK");
			} else {
				map.put("String", "ERROR");
			}
		}
		return map;
	}
	

	@ResponseBody
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public Map<String, Object> modify(@RequestBody User userInfo,
			HttpSession httpSession) {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User) httpSession.getAttribute("user");
		System.out.println("--------" + user.getEmail());
		user.setPassword(userInfo.getPassword());
		System.out.println("==========" + user.getEmail());
		if (userManager.updateUser(user)) {
			map.put("states", "OK");
			map.put("user", userInfo);
		} else {
			map.put("states", "ERROR");
			map.put("user", userInfo);
		}
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/checkLogin", method = { RequestMethod.POST })
	public Map<String, Object> checkLogin(@RequestBody String fname,
			HttpSession httpSession) {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User) httpSession.getAttribute("user");
		if (user != null) {
			map.put("states", "OK");
			map.put("user", user);
		} else {
			map.put("states", "ERROR");
			map.put("user", null);
		}
		return map;
	}

	@ResponseBody
	@RequestMapping("/logout")
	public Map<String, Object> modify(HttpSession httpSession) {
		Map<String, Object> map = new HashMap<String, Object>();
		if ((User) httpSession.getAttribute("user") != null) {
			httpSession.removeAttribute("user");
			httpSession.invalidate();
			map.put("state", "OK");
		} else {
			map.put("state", "ERROR");
		}
		return map;
	}
}
